package com.midea.ai.air.ui.device.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.midea.ai.air.R;
import com.midea.ai.air.ui.device.data.DeviceGridItem;

import java.util.ArrayList;

/**
 * Created by ouyangdi on 2015/12/9.
 * "家电"页面，家电显示GridView的Adapter
 */
public class DeviceGridAdapter extends ArrayAdapter<DeviceGridItem> {

    private Context mContext;

    public DeviceGridAdapter(Context context, int resource, ArrayList<DeviceGridItem> gridItemsList) {
        super(context, resource, gridItemsList);
        this.mContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        DeviceGridItem item = getItem(position);

        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item_device,null);
            holder = new ViewHolder();
            holder.txtDevice = (TextView)convertView.findViewById(R.id.txtView_device_name);
            holder.txtLoc = (TextView)convertView.findViewById(R.id.txtView_location);
            holder.imgViewDevice = (ImageView)convertView.findViewById(R.id.imgView_device);
            holder.rightLine = convertView.findViewById(R.id.view_right_line);
            holder.botLine = convertView.findViewById(R.id.view_bot_line);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder)convertView.getTag();
        }

        holder.txtDevice.setText(item.getStrDeviceName());

        holder.txtLoc.setText(item.getStrLocation());

        holder.imgViewDevice.setImageResource(item.getResIdImg());

        if(position%2==0) {
            holder.rightLine.setVisibility(View.VISIBLE);
            holder.botLine.setVisibility(View.VISIBLE);

        } else {
            holder.botLine.setVisibility(View.VISIBLE);
        }

        return convertView;
    }


    public class ViewHolder {
        public TextView txtDevice;
        public TextView txtLoc;
        public ImageView imgViewDevice;
        public View rightLine;
        public View botLine;
    }
}
